package com.base.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.mapper.voipmapper.UsersMapper;
import com.base.po.sqlparam.UsersExamplePage;
import com.base.po.voip.Users;
import com.base.po.voip.UsersExample;
import com.base.service.UsersService;
import com.base.utils.Checked;
import com.base.utils.Page;
import com.base.utils.Produce;
import org.springframework.stereotype.Service;

@Service("usersService")
public class UsersServiceImpl implements UsersService {
	@Autowired
	private UsersMapper usersmapper;

	@Override
	public int deleteByPrimaryKey(String id) {
		return usersmapper.deleteByPrimaryKey(Checked.checkedString(id));
	}

	@Override
	public int insert(String id, String username, String password, String email, Date logintime, String loginip,
					  Integer role, Boolean datastatus, String mobile, BigDecimal blance, String freeroute, String parentid,
					  String agentlogo, String prefix, String theme, Boolean addcard, Boolean nextagent, Byte airrecharge, String androidversion,
					  String androidpath, String iosversion, String iospath, Integer regimoney, Integer regishopimoney, Integer regitime,
					  Date endtime, String pic, BigDecimal dkbl, Boolean isshop, Integer type, String shopid, String adid,
					  BigDecimal profitratio, String appkey, String appsecret, BigDecimal linebalance, String pid, String dappkey,
					  String sqappkey, Integer overdraft, String appstore, String appid, String keypath, String agentprefix,
					  String oneagent, Boolean sxxh, Boolean isauth, Boolean isalipay, Boolean isprofit, Boolean isaliprofit,
					  BigDecimal aliprofitratio, BigDecimal aliprofitmoney, BigDecimal profitmoney, Integer shopblance,
					  Integer igtype, Integer calltype, Byte isgrounding) {
		Users users = new Users(id, username, password, email, Produce.getNowDate(), logintime, loginip, role,
				datastatus, mobile, blance, freeroute, parentid, agentlogo, prefix, theme, addcard, nextagent, airrecharge,
				androidversion, androidpath, iosversion, iospath, regimoney, regishopimoney, regitime, endtime, pic, dkbl, isshop,
				type, shopid, adid, profitratio, appkey, appsecret, linebalance, pid, dappkey, sqappkey, overdraft,
				appstore, appid, keypath, agentprefix, oneagent, sxxh, isauth, isalipay, isprofit, isaliprofit,
				aliprofitratio, aliprofitmoney, profitmoney, shopblance, igtype, calltype, isgrounding);
		return usersmapper.insert(users);
	}

	@Override
	public int updateByPrimaryKeySelective(Users record) {
		return usersmapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Users selectByPrimaryKey(String id) {
		if (id == null || id.isEmpty()) {
			return null;
		}
		return usersmapper.selectByPrimaryKey(id);
	}

	@Override
	public Users selectsomeByPrimaryKey(String id) {
		return usersmapper.selectsomeByPrimaryKey(id);
	}

	@Override
	public Users selectByUsername(String username) {
		if (username == null || username.isEmpty()) {
			return null;
		}
		return usersmapper.selectByUsername(username);
	}

	@Override
	public List<Users> selectByExamplePage(UsersExamplePage examplePage) {
		return usersmapper.selectByExamplePage(examplePage);
	}

	@Override
	public int countByExample(UsersExample example) {
		return usersmapper.countByExample(example);
	}

	@Override
	public int deleteByExample(UsersExample example) {
		return usersmapper.deleteByExample(example);
	}

	@Override
	public int insert(Users record) {
		return usersmapper.insert(record);
	}

	@Override
	public int insertSelective(Users record) {
		return usersmapper.insertSelective(record);
	}

	@Override
	public List<Users> selectByExample(UsersExample example) {
		return usersmapper.selectByExample(example);
	}

	@Override
	public int updateByExampleSelective(Users record, UsersExample example) {
		return usersmapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(Users record, UsersExample example) {
		return usersmapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKey(Users record) {
		return usersmapper.updateByPrimaryKey(record);
	}

	@Override
	public List<String> selectByParentId(String parentId) {

		return usersmapper.selectByParentId(parentId);
	}

	@Override
	public List<String> selectByShopId(String shopId) {

		return usersmapper.selectByShopId(shopId);
	}

	@Override
	public List<Users> selectByUserName(String username) {

		return usersmapper.selectByUserName(username);
	}

	@Override
	public List<String> selectUserNameByParentId(String parentId) {

		return usersmapper.selectUserNameByParentId(parentId);
	}

	@Override
	public List<String> selectUserNameByShopId(String shopId) {

		return usersmapper.selectUserNameByShopId(shopId);
	}

	@Override
	public List<Users> selectByParentID(Page page, String parentid, String username, String pId, String sId,
										Boolean datastatus) {

		return usersmapper.selectByParentID(page, parentid, username, pId, sId, datastatus);
	}

	@Override
	public int countByParentID(String parentid, String username, String pId, String sId, Boolean datastatus) {

		return usersmapper.countByParentID(parentid, username, pId, sId, datastatus);
	}

	@Override
	public List<Users> selectByAll(Page page, String username, String pId, String sId, Boolean datastatus) {

		return usersmapper.selectByAll(page, username, pId, sId, datastatus);
	}

	@Override
	public int countByAll(String username, String pId, String sId, Boolean datastatus) {

		return usersmapper.countByAll(username, pId, sId, datastatus);
	}

	@Override
	public List<Users> selectByShopID(Page page, String shopid, String id, String pId, String sId, Boolean datastatus) {

		return usersmapper.selectByShopID(page, shopid, id, pId, sId, datastatus);
	}

	@Override
	public int countByShopID(String shopid, String id, String pId, String sId, Boolean datastatus) {

		return usersmapper.countByShopID(shopid, id, pId, sId, datastatus);
	}

	@Override
	public List<Users> selectByShopIDNotAll(Page page, String shopid, String username, String pId, String sId,
											Boolean datastatus) {

		return usersmapper.selectByShopIDNotAll(page, shopid, username, pId, sId, datastatus);
	}

	@Override
	public int countByShopIDNotAll(String shopid, String username, String pId, String sId, Boolean datastatus) {

		return usersmapper.countByShopIDNotAll(shopid, username, pId, sId, datastatus);
	}

	@Override
	public List<Users> selectByUsers(Users record) {
		return usersmapper.selectByUsers(record);
	}

	@Override
	public List<String> selectParentIdByShopiId(String shopid) {

		return usersmapper.selectParentIdByShopiId(shopid);
	}

	@Override
	public List<Users> selectOEMByAll(Page page, String username, Boolean datastatus) {

		return usersmapper.selectOEMByAll(page, username, datastatus);
	}

	@Override
	public List<Users> selectOEMByParentID(Page page, String parentid, String username, Boolean datastatus) {

		return usersmapper.selectOEMByParentID(page, parentid, username, datastatus);
	}

	@Override
	public int countOEMByAll(String username, Boolean datastatus) {

		return usersmapper.countOEMByAll(username, datastatus);
	}

	@Override
	public int countOEMByParentID(String parentid, String username, Boolean datastatus) {

		return usersmapper.countOEMByParentID(parentid, username, datastatus);
	}

	@Override
	public Users selectShopByUsername(String selfShopName) {
		return usersmapper.selectByshopName(selfShopName);
	}

	@Override
	public List<Users> selectUsersByRole(int role) {
		return usersmapper.selectUsersByRole(role);
	}

	@Override
	public int hasUsers(String parentId) {
		return usersmapper.hasUsers(parentId);
	}

	@Override
	public String selectAllChildByShopId(String adminUserId) {
		return usersmapper.selectAllChildByShopId(adminUserId);
	}

	@Override
	public List<String> findChildList(String shopId) {
		// 下级商家
		List<String> lowerLevelShopId = usersmapper.selectForLowerLevel(shopId);
		// 查询的用户
		List<String> findLowerLevelShopId = new ArrayList<>();
		// 获取所有用户
		List<String> teamLowerLevelShopId = new ArrayList<>();
		teamLowerLevelShopId.addAll(lowerLevelShopId);

		boolean flag = true;
		int lastSize = 0;//下面逻辑查找该用户全部下级长度
		int nowSize = 0;
		while (flag) {
			if (lowerLevelShopId.size() > 0) {
				findLowerLevelShopId = usersmapper.selectForLowerLevelByList(lowerLevelShopId);

				teamLowerLevelShopId.addAll(findLowerLevelShopId);

				lowerLevelShopId = findLowerLevelShopId;
				nowSize = teamLowerLevelShopId.size();
				if (nowSize == lastSize) {
					flag = false;
				} else {
					lastSize = nowSize;
				}
			} else {
				flag = false;
			}
		}

		return teamLowerLevelShopId;
	}

	@Override
	public List<Users> selectByPid(String parentId) {

		return usersmapper.selectByPid(parentId);
	}

	@Override
	public List<String> selectAllShop(String id) {

		return usersmapper.selectAllShop(id);
	}

}